//example for using the mina library 
#include "tcp_handler.h"
#include "tcp_acceptor.h"
#include "tcp_acceptor_config.h"
#include <boost/shared_ptr.hpp>
#include "tcp_package_filter.h"

#include "udp_acceptor.h"
#include "udp_handler.h"
#include "udp_acceptor_config.h"

#include "tcp_connector.h"
#include "tcp_connector_config.h"
#include <string>
#include <stdio.h>
using namespace vpm::mina;

class MyPackageFilter
{
    public:
    int operator ()( char * pData, int len)
    {
      //  printf ("my filter processed \n"); 
        if (len >=2 )
        {
            return 2; 
        }
        return -1; 
    }
}; 
class MyHandler : public TcpHandler 
{
    public:
        virtual void message_received(IoSession & session,MessageContext & msg)
        {
            TcpMessageContext & tcpMsg = (TcpMessageContext &) msg; 

			char szBuf[1024]= {0}; 
			memcpy(szBuf,tcpMsg.valid_data(), tcpMsg.valid_length()); 

            printf("received data %s , length %d\n", szBuf, tcpMsg.valid_length()); 
        }
}; 


int main(int, char *[])
{

	MyHandler tcpHandler;
	TcpAcceptorConfigPtr config(new TcpAcceptorConfig()); 
    config->set_tcp_package_parser<MyPackageFilter>(); 

	IoAcceptor * pAcceptor = new TcpAcceptor<>(config,tcpHandler);

	pAcceptor->bind(ServiceUrl("tcp://0.0.0.0:8000"));

	UdpHandler udpHandler; 
	UdpAcceptorConfigPtr udpConfig(new UdpAcceptorConfig()); 
	IoAcceptor * pUdpAcceptor = new UdpAcceptor(udpConfig, udpHandler);
	pUdpAcceptor->bind(ServiceUrl("udp://0.0.0.0:9000"));

	//TcpConnectorConfig tcpConnectorConfig; 
	//IoConnector * pTcpConnector = new TcpConnector(tcpConnectorConfig,tcpHandler); 
	//pTcpConnector->connect(ServiceUrl("tcp://172.24.202.106:9000"));
	

	//while (true)
	{
#ifdef WIN32
		Sleep(100000);
#else 
        sleep(1000); 
#endif //
	}
	int ch = getchar(); 

	return 0; 
}
